package com.sany.taskmanager.ui.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.sany.taskmanager.R;
import com.sany.taskmanager.bean.TaskGroupBean;
import com.sany.taskmanager.bean.TaskInfoBean;
import com.sany.taskmanager.databinding.ItemGroupTaskSendedBinding;
import com.sany.taskmanager.databinding.ItemTaskSendedBinding;

import java.util.List;

/**
 * 已任务下发列表
 */
public class TaskSendedAdapter extends BaseExpandableRecyclerViewAdapter<TaskGroupBean, TaskInfoBean, TaskSendedAdapter.GroupVH, TaskSendedAdapter.ChildVH> {

    private final Context mContext;
    private List<TaskGroupBean> mTaskList;
    private OnOperateListener mOnOperateListener;

    public void setTaskList(List<TaskGroupBean> list) {
        mTaskList = list;
        notifyDataSetChanged();
    }

    public void setOnOperateListener(OnOperateListener onOperateListener) {
        mOnOperateListener = onOperateListener;
    }

    public TaskSendedAdapter(Context context) {
        mContext = context;
    }

    @Override
    public int getGroupCount() {
        return mTaskList == null ? 0 : mTaskList.size();
    }

    @Override
    public TaskGroupBean getGroupItem(int groupIndex) {
        return mTaskList.get(groupIndex);
    }

    @Override
    public GroupVH onCreateGroupViewHolder(ViewGroup parent, int groupViewType) {
        ItemGroupTaskSendedBinding itemHistoryTaskBinding = ItemGroupTaskSendedBinding.inflate(LayoutInflater.from(mContext), parent, false);
        return new TaskSendedAdapter.GroupVH(itemHistoryTaskBinding);
    }

    @Override
    public void onBindGroupViewHolder(GroupVH holder, TaskGroupBean groupBean, boolean isExpanding) {

        if (groupBean.isExpandable()) {
            holder.ivArrow.setVisibility(View.VISIBLE);
            holder.ivArrow.setImageResource(isExpanding ? R.mipmap.icon_arrow_expanding : R.mipmap.icon_arrow_folding);
        } else {
            holder.ivArrow.setVisibility(View.GONE);
        }

        TaskInfoBean item = groupBean.getHeaderBean();
        if (groupBean.getList() == null || groupBean.getList().isEmpty()) {
            holder.tvNum.setText(String.valueOf(mTaskList.indexOf(groupBean) + 1));
        } else {
            holder.tvNum.setText(String.format(mContext.getString(R.string.format_group_boom), mTaskList.indexOf(groupBean) + 1));
        }
        holder.tvMaterialNum.setText(item.getMaterialNum());
        holder.tvBoomColor.setText(item.getColorCode());
        holder.tvBoomHeadColor.setText(item.getHeadColorCode());
        holder.tvCount.setText("1");
        holder.tvSendTime.setText(item.getSendTime());
        holder.tvNewBoom.setText(item.getBoomType() == 1 ? mContext.getString(R.string.label_yes) : mContext.getString(R.string.label_empty));
        holder.tvCarModel.setText(TextUtils.isEmpty(item.getCarModelNum()) ? mContext.getString(R.string.label_empty) : item.getCarModelNum());

        holder.tvModify.setOnClickListener((v) -> {
            if (mOnOperateListener != null) {
                mOnOperateListener.modify(holder.getAdapterPosition());
            }
        });
        holder.tvCancel.setOnClickListener((v) -> {
            if (mOnOperateListener != null) {
                mOnOperateListener.cancel(item);
            }
        });
    }

    @Override
    public ChildVH onCreateChildViewHolder(ViewGroup parent, int childViewType) {
        ItemTaskSendedBinding itemHistoryTaskBinding = ItemTaskSendedBinding.inflate(LayoutInflater.from(mContext), parent, false);
        return new TaskSendedAdapter.ChildVH(itemHistoryTaskBinding);
    }

    @Override
    public void onBindChildViewHolder(ChildVH holder, TaskGroupBean groupBean, TaskInfoBean item) {
        holder.tvMaterialNum.setText(item.getMaterialNum());
        holder.tvBoomColor.setText(item.getColorCode());
        holder.tvBoomHeadColor.setText(item.getHeadColorCode());
        holder.tvCount.setText("1");
        holder.tvSendTime.setText(item.getSendTime());
        holder.tvNewBoom.setText(item.getBoomType() == 1 ? mContext.getString(R.string.label_yes) : mContext.getString(R.string.label_empty));
        holder.tvCarModel.setText(TextUtils.isEmpty(item.getCarModelNum()) ? mContext.getString(R.string.label_empty) : item.getCarModelNum());

        holder.tvModify.setOnClickListener((v) -> {
            if (mOnOperateListener != null) {
                mOnOperateListener.modify(holder.getAdapterPosition());
            }
        });
        holder.tvCancel.setOnClickListener((v) -> {
            if (mOnOperateListener != null) {
                mOnOperateListener.cancel(item);
            }
        });
    }

    static class ChildVH extends RecyclerView.ViewHolder {

        public TextView tvNum;
        public TextView tvMaterialNum;
        public TextView tvBoomColor;
        public TextView tvBoomHeadColor;
        public TextView tvCount;
        public TextView tvNewBoom;
        public TextView tvSendTime;
        public TextView tvModify;
        public TextView tvCancel;
        public TextView tvCarModel;

        public ChildVH(@NonNull ItemTaskSendedBinding itemTaskSendedBinding) {
            super(itemTaskSendedBinding.getRoot());
            tvNum = itemTaskSendedBinding.tvNum;
            tvMaterialNum = itemTaskSendedBinding.tvMaterialNum;
            tvBoomColor = itemTaskSendedBinding.tvColor;
            tvBoomHeadColor = itemTaskSendedBinding.tvHeadColor;
            tvCount = itemTaskSendedBinding.tvCount;
            tvSendTime = itemTaskSendedBinding.tvSendTime;
            tvModify = itemTaskSendedBinding.tvModify;
            tvCancel = itemTaskSendedBinding.tvCancel;
            tvNewBoom = itemTaskSendedBinding.tvNewBoom;
            tvCarModel = itemTaskSendedBinding.tvCarModel;
        }
    }

    static class GroupVH extends BaseExpandableRecyclerViewAdapter.BaseGroupViewHolder {

        public TextView tvNum;
        public TextView tvMaterialNum;
        public TextView tvBoomColor;
        public TextView tvBoomHeadColor;
        public TextView tvCount;
        public TextView tvNewBoom;
        public TextView tvSendTime;
        public TextView tvModify;
        public TextView tvCancel;
        public ImageView ivArrow;
        public TextView tvCarModel;

        public GroupVH(@NonNull ItemGroupTaskSendedBinding itemTaskSendedBinding) {
            super(itemTaskSendedBinding.getRoot());
            tvNum = itemTaskSendedBinding.tvNum;
            tvMaterialNum = itemTaskSendedBinding.tvMaterialNum;
            tvBoomColor = itemTaskSendedBinding.tvColor;
            tvBoomHeadColor = itemTaskSendedBinding.tvHeadColor;
            tvCount = itemTaskSendedBinding.tvCount;
            tvSendTime = itemTaskSendedBinding.tvSendTime;
            tvModify = itemTaskSendedBinding.tvModify;
            tvCancel = itemTaskSendedBinding.tvCancel;
            tvNewBoom = itemTaskSendedBinding.tvNewBoom;
            ivArrow = itemTaskSendedBinding.ivArrow;
            tvCarModel = itemTaskSendedBinding.tvCarModel;
        }

        @Override
        protected void onExpandStatusChanged(RecyclerView.Adapter relatedAdapter, boolean isExpanding) {
            ivArrow.setImageResource(isExpanding ? R.mipmap.icon_arrow_expanding : R.mipmap.icon_arrow_folding);
        }
    }

    public interface OnOperateListener {
        void modify(int position);

        void cancel(TaskInfoBean item);
    }

}
